<?php 
include("../connect/connect.php");
include('../function/thaidate.php');
include('../function/function.php');
$id = $_GET['id'];

	$xQuery = mysql_query("SELECT * FROM deposit,reservation_detail WHERE deposit.reservation_detail_id = reservation_detail.reservation_detail_id AND deposit.reservation_detail_id = '$id'");
	$xRow = mysql_fetch_array($xQuery);
        

//แสดงข้อมูลเงินมัดจำ
switch ($xRow['deposit_type']) {
    case cash:
        $tbl = 'deposit_cash';
        $total = 'deposit_cash_total';
        break;
    case transfer:
        $tbl = 'deposit_transfer';
        $total = 'deposit_transfer_total';
        break;
    case check:
        $tbl = 'deposit_check';
        $total = 'deposit_check_total';
        break;
}

$zQuery = mysql_query("SELECT * FROM $tbl WHERE deposit_id = '".$xRow['deposit_id']."'");
$zRow = mysql_fetch_array($zQuery);
$totalA = $zRow[$total];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ใบรับเงินมัดจำการจองห้องพัก</title>
<link rel="shortcut icon" href="../image/Letter-T.ico" />
<script language=javascript>
function Print() {
	document.getElementById('endsale2').disabled = false;
}
</script>
<style type="text/css" media="print">
.FromPrint {
	border: 0px;
	padding: 0px;
}
input {
	display: none;
}
body{
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
}
.fontB {
	font-weight: bold;
}
.headF {
	font-weight: bold;
	font-size: 16px;
	padding: 10px 0px 15px 0px;
}
.list {
	border-collapse: collapse;
	width: 100%;
	border-top: 1px solid #DDDDDD;
	border-left: 1px solid #DDDDDD;
	margin-bottom: 20px;
	margin-left: 5px;
}

.list td {
	border-right: 1px solid #DDDDDD;
	border-bottom: 1px solid #DDDDDD;
}

.list thead td {
	background-color: #EFEFEF;
	padding: 0px;
}

.list thead td a,.list thead td {
	text-decoration: none;
	color: #222222;
	font-weight: bold;
}

.list tbody a {
	text-decoration: underline;
}

.list tbody td {
	vertical-align: middle;
	padding: 0px;
}

.list tbody tr:odd {
	background: #FFFFFF;
}

.list tbody tr:even {
	background: #E4EEF7;
}

.list .left {
	text-align: left;
	padding: 5px;
}

.list .right {
	text-align: right;
	padding: 5px;
}

.list .center {
	text-align: center;
	padding: 5px;
}

.list .pRight {
	text-align: right;
	padding-right: 20px;
}

.list .pLeft {
	text-align: left;
	padding-left: 20px;
}
</style>

<style type="text/css" media="screen">
body{
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
}
.fontB {
	font-weight: bold;
}
.headF {
	font-weight: bold;
	font-size: 15px;
	padding: 10px 0px 10px 0px;
}
.list {
	border-collapse: collapse;
	width: 100%;
	border-top: 1px solid #DDDDDD;
	border-left: 1px solid #DDDDDD;
	margin-bottom: 20px;
	margin-left: 5px;
}

.list td {
	border-right: 1px solid #DDDDDD;
	border-bottom: 1px solid #DDDDDD;
}

.list thead td {
	background-color: #EFEFEF;
	padding: 0px;
}

.list thead td a,.list thead td {
	text-decoration: none;
	color: #222222;
	font-weight: bold;
}

.list tbody a {
	text-decoration: underline;
}

.list tbody td {
	vertical-align: middle;
	padding: 0px;
}

.list tbody tr:odd {
	background: #FFFFFF;
}

.list tbody tr:even {
	background: #E4EEF7;
}

.list .left {
	text-align: left;
	padding: 5px;
}

.list .right {
	text-align: right;
	padding: 5px;
}

.list .center {
	text-align: center;
	padding: 5px;
}

.list .pRight {
	text-align: right;
	padding-right: 20px;
}

.list .pLeft {
	text-align: left;
	padding-left: 20px;
}
.FromPrint {
	border: 1px dotted; 
	width: 500px; 
	padding: 30px;
}
input {
	font-size: 17px; 
	width: 100px; 
	height:30px;
}
</style>
</head>
<body>
<div align="center">
<div align="left" class="FromPrint">
<div style="font-size: 20px; text-align:center;font-weight: bold; padding-bottom: 30px;">ใบรับเงินมัดจำการจองห้องพัก</div>
<div align="right">
	<b>วันที่รับเงินมัดจำ</b> <?php echo thai_date2(strtotime($xRow['deposit_date']))." <b>เวลา</b> ".date("H:i:s",strtotime($xRow['deposit_date']));?><br />
	<b>ผู้รับเงินมัดจำ</b> <?php echo $xRow['deposit_name'];?> <?php if($xRow['deposit_phone']!=""){echo "<b>โทร.</b> ".$xRow['deposit_phone'];}?><br />
	<b>ผู้บันทึกรายการ</b>
</div>
   <table class="form">
       <tr>
        <td class="fontB">ชื่อ-นามสกุล : </td>
        <td><?php echo $xRow['reservation_detail_fname'];?> <?php echo $xRow['reservation_detail_lname'];?></td>
      </tr>    
       <tr>
        <td class="fontB">ชื่อบริษัท : </td>
        <td><?php echo $xRow['reservation_detail_name'];?></td>
      </tr>   
       <tr>
        <td class="fontB">เบอร์โทรศัพท์ : </td>
        <td><?php echo $xRow['reservation_detail_phone'];?></td>
      </tr>   
       <tr>
        <td class="fontB">เบอร์แฟ็กซ์ : </td>
        <td><?php echo $xRow['reservation_detail_fax'];?></td>
      </tr>   
       <tr>
        <td class="fontB">อีเมล์ : </td>
        <td><?php echo $xRow['reservation_detail_email'];?></td>
      </tr>                                
    </table>
<hr>

<div class="headF">ข้อมูลการชำระเงิน</div>
<div style="width:100%;">
<?php
//กรณีเลือกบริษัททัวร์
if($xRow['reservation_detail_payment_information']=='special'){
	$gValue = $xRow['group_detail_id'];
	$rQuery = mysql_query("SELECT roomtype.roomtype_id as roomtype,roomtype.roomtype_name as roomtype_name,count(roomtype.roomtype_id) as roomtype_id,group_detail_room.group_detail_room_price as group_detail_room_price FROM reservation,roomtype,room,group_detail,group_detail_room WHERE group_detail_room.group_detail_id = group_detail.group_detail_id AND group_detail_room.roomtype_id = roomtype.roomtype_id AND room.roomtype_id=group_detail_room.roomtype_id AND room.room_id=reservation.room_id AND group_detail.group_detail_id = '".$gValue."' AND reservation.reservation_detail_id = '$id' GROUP BY roomtype.roomtype_id ORDER BY roomtype.roomtype_name");

	$sQuery = mysql_query("SELECT group_detail_name FROM group_detail WHERE group_detail_id = '".$gValue."'");
	$sRow = mysql_fetch_array($sQuery);
?>
<h4>บริษัท <?php echo $sRow['group_detail_name'];?></h4>
<table class="list">
<thead>
  <tr>
    <td class="center">ลำดับ</td>
    <td class="center">รายการ</td>
    <td class="center">จำนวน</td>
    <td class="center">ราคา/หน่วย</td>
    <td class="center">รวม</td>
  </tr>
</thead>
<?php 
$num=0;
while($rRow = mysql_fetch_array($rQuery)){
	$num++;
?>
  <tr>
    <td class="center"><?php echo $num;?></td>
    <td class="pLeft">
    <?php echo $rRow['roomtype_name']." ";
    	$RmQuery = mysql_query("SELECT room.room_name as room_name FROM room,reservation,roomtype WHERE reservation.reservation_detail_id = '$id' AND room.roomtype_id=roomtype.roomtype_id AND room.room_id = reservation.room_id AND roomtype.roomtype_id = '".$rRow['roomtype']."' GROUP BY room.room_id");
    	while($RmRow = mysql_fetch_array($RmQuery)){
    		$room_name .= ", ".$RmRow['room_name'];
    	}
    		echo "(".substr($room_name, 2).")";
		$room_name = "";
    ?></td>
    <td class="pRight"><?php echo $rRow['roomtype_id'];?></td>
    <td class="pRight"><?php echo bath_format($rRow['group_detail_room_price']);?></td>
    <td class="pRight"><?php echo bath_format($rRow['roomtype_id']*$rRow['group_detail_room_price']);?></td>
  </tr>
<?php 
	$strtal = $strtal+($rRow['roomtype_id']*$rRow['group_detail_room_price']);
}
}

//กรณีเลือก W/N
if($xRow['reservation_detail_payment_information']=='walkin'){
	$rQuery = mysql_query("SELECT roomtype.roomtype_id as roomtype,roomtype_name,count(roomtype.roomtype_id) as roomtype_id,roomtype.roomtype_price as roomtype_price FROM reservation,roomtype,room WHERE reservation.reservation_detail_id = '$id' AND room.roomtype_id=roomtype.roomtype_id AND room.room_id=reservation.room_id GROUP BY roomtype.roomtype_id ORDER BY roomtype.roomtype_name");
	$discount = $_POST['discount'];
	$discount2 = $_POST['discount2'];

?>
<!-- แสดงรายการ Walk-in -->
<h3>W/N</h3>
<table class="list">
<thead>
  <tr>
    <td class="center">ลำดับ</td>
    <td class="center">รายการ</td>
    <td class="center">จำนวน</td>
    <td class="center">ราคา/หน่วย</td>
    <td class="center">รวม</td>
  </tr>
</thead>
<?php 
$num=0;
while($rRow = mysql_fetch_array($rQuery)){
	$num++;
?>
  <tr>
    <td class="center"><?php echo $num;?></td>
    <td class="pLeft">
    <?php echo $rRow['roomtype_name']." ";
    	$RmQuery = mysql_query("SELECT room.room_name as room_name FROM room,reservation,roomtype WHERE reservation.reservation_detail_id = '$id' AND room.roomtype_id=roomtype.roomtype_id AND room.room_id = reservation.room_id AND roomtype.roomtype_id = '".$rRow['roomtype']."' GROUP BY room.room_id");
    	while($RmRow = mysql_fetch_array($RmQuery)){
    		$room_name .= ", ".$RmRow['room_name'];
    	}
    		echo "(".substr($room_name, 2).")";
		$room_name = "";
    ?></td>
    <td class="pRight"><?php echo $rRow['roomtype_id'];?></td>
    <td class="pRight"><?php echo bath_format($rRow['roomtype_price']);?></td>
    <td class="pRight"><?php echo bath_format($rRow['roomtype_id']*$rRow['roomtype_price']);?></td>
  </tr>
<?php 
	$strtal = $strtal+($rRow['roomtype_id']*$rRow['roomtype_price']);
}
}
?>
<?php 
//กรณีเลือกกรุ๊ป
if($xRow['reservation_detail_payment_information']=='group'){

	$gQuery = mysql_query("SELECT * FROM reservation_group WHERE reservation_detail_id = '$id'");
?>
<h3>กรุ๊ป</h3>
<table class="list">
<thead>
  <tr>
    <td class="center" width="1">ลำดับ</td>
    <td class="center">รายการ</td>
    <td class="center" width="13%">จำนวน</td>
    <td class="center" width="13%">ราคา/หน่วย</td>
    <td class="center" width="18%">รวม</td>
  </tr>
</thead>
<?php 
	$cCount = 0;
	while($gRow = mysql_fetch_array($gQuery)){
		$cCount++;
?>
  <tr>
    <td class="center"><?php echo $cCount;?></td>
    <td class="pLeft"><?php echo $gRow['reservation_group_list'];?></td>
    <td class="pRight"><?php echo $gRow['reservation_group_amount'];?></td>
    <td class="pRight"><?php echo bath_format($gRow['reservation_group_price']);?></td>
    <td class="pRight"><?php echo bath_format($gRow['reservation_group_amount']*$gRow['reservation_group_price']);?></td>
  </tr>
<?php 
	$strtal = $strtal+($gRow['reservation_group_amount']*$gRow['reservation_group_price']);
	}
?>
  <tr>
	<td colspan="4" class="right">รวมเงิน</td>
	<td class="pRight"><?php echo bath_format($strtal);?></td>
  </tr>
  <tr>
      <td colspan="4" class="right"><font style="border-bottom: 1px solid #000000;"><b>ค่าเงินมัดจำ</b></font></td>
    <td class="pRight"><font style="border-bottom: 1px solid #000000;"><b>- <?php echo bath_format($totalA); ?> </b></font></td>
</tr>
    <tr>
        <td colspan="4" class="right"><font style="border-bottom: 3px double #000000;"><b>เหลือเงินค้างจ่าย</b></font></td>
        <td class="pRight"><font style="border-bottom: 3px double #000000;"><b><?php $Atotal = $strtal-$totalA; echo bath_format($Atotal); ?></b></font></td>
    </tr>
</table>
<?php
}
?>


<?php 
//กรณีเลือก Special กับ Walkin
$swQuery = mysql_query("SELECT reservation_detail_payment_information FROM reservation_detail WHERE reservation_detail_id = '$id'");
$swRow = mysql_fetch_array($swQuery);
if($swRow['reservation_detail_payment_information']=='special' || $swRow['reservation_detail_payment_information']=='walkin'){
?>
<?php
/*
1 = รวมอาหารเช้า
2 = Extra Bed + ABF
3 = Extra Bed
4 = ABF (ผู้ใหญ่)
5 = ABF (เด็ก)
6 = พักเพิ่ม (ผู้ใหญ่)
7 = พักเพิ่ม (เด็ก)
*/
//เช็คข้อมูลจากตาราง reservation_information ว่าเลือก more_information_id อะไรเพื่อนำข้อมูลมาแสดง
$StAbfQuery = mysql_query("SELECT more_information_value,more_information_price FROM reservation_information WHERE reservation_detail_id = '$id' AND more_information_id = '1'");
$StAbfRow = mysql_fetch_array($StAbfQuery);
if($StAbfRow){
	$NumtABF = 1;
?>
  <tr>
    <td class="center"><?php echo $num+$NumtABF;?></td>
    <td class="pLeft"><?php echo "รวมอาหารเช้า";?></td>
    <td class="pRight"><?php echo $StAbfRow['more_information_value'];?></td>
    <td class="pRight"><?php echo bath_format($StAbfRow['more_information_price']);?></td>
    <td class="pRight"><?php $tABFTotal = $StAbfRow['more_information_value']*$StAbfRow['more_information_price']; echo bath_format($tABFTotal);?></td>
  </tr>
<?php } ?>
<?php 
//กรณีหากมีข้อมูล Extra Bed + ABF
$SExtraAbfQuery = mysql_query("SELECT more_information_value,more_information_price FROM reservation_information WHERE reservation_detail_id = '$id' AND more_information_id = '2'");
$SExtraAbfRow = mysql_fetch_array($SExtraAbfQuery);
if($SExtraAbfRow){
	$NumExtraAbf = 1;
?>
  <tr>
    <td class="center"><?php echo $num+$NumExtraAbf+$NumtABF;?></td>
    <td class="pLeft"><?php echo "Extra Bed + ABF";?></td>
    <td class="pRight"><?php echo $SExtraAbfRow['more_information_value'];?></td>
    <td class="pRight"><?php echo bath_format($SExtraAbfRow['more_information_price']);?></td>
    <td class="pRight"><?php $ExtraAbfTotal = $SExtraAbfRow['more_information_value']*$SExtraAbfRow['more_information_price']; echo bath_format($ExtraAbfTotal);?></td>
  </tr>
<?php } ?>
<?php 
//กรณีหากมีข้อมูล Extra Bed
$SExtraQuery = mysql_query("SELECT more_information_value,more_information_price FROM reservation_information WHERE reservation_detail_id = '$id' AND more_information_id = '3'");
$SExtraRow = mysql_fetch_array($SExtraQuery);
if($SExtraRow){
	$NumExtra = 1;
?>
  <tr>
    <td class="center"><?php echo $num+$NumExtraAbf+$NumtABF+$NumExtra;?></td>
    <td class="pLeft"><?php echo "Extra Bed";?></td>
    <td class="pRight"><?php echo $SExtraRow['more_information_value'];?></td>
    <td class="pRight"><?php echo bath_format($SExtraRow['more_information_price']);?></td>
    <td class="pRight"><?php $ExtraTotal = $SExtraRow['more_information_value']*$SExtraRow['more_information_price']; echo bath_format($ExtraTotal);?></td>
  </tr>
<?php } ?>
<?php 
//กรณีหากมีข้อมูล ABF (ผู้ใหญ่)
$AbfAQuery = mysql_query("SELECT more_information_value,more_information_price FROM reservation_information WHERE reservation_detail_id = '$id' AND more_information_id = '4'");
$AbfARow = mysql_fetch_array($AbfAQuery);
if($AbfARow){
	$NumAbfA = 1;
?>
  <tr>
    <td class="center"><?php echo $num+$NumExtraAbf+$NumtABF+$NumExtra+$NumAbfA;?></td>
    <td class="pLeft"><?php echo "ABF (ผู้ใหญ่)";?></td>
    <td class="pRight"><?php echo $AbfARow['more_information_value'];?></td>
    <td class="pRight"><?php echo bath_format($AbfARow['more_information_price']);?></td>
    <td class="pRight"><?php $AbfATotal = $AbfARow['more_information_value']*$AbfARow['more_information_price']; echo bath_format($AbfATotal);?></td>
  </tr>
<?php } ?>
<?php 
//กรณีหากมีข้อมูล ABF (เด็ก)
$AbfCQuery = mysql_query("SELECT more_information_value,more_information_price FROM reservation_information WHERE reservation_detail_id = '$id' AND more_information_id = '5'");
$AbfCRow = mysql_fetch_array($AbfCQuery);
if($AbfCRow){
	$NumAbfC = 1;
?>
  <tr>
    <td class="center"><?php echo $num+$NumExtraAbf+$NumtABF+$NumExtra+$NumAbfA+$NumAbfC;?></td>
    <td class="pLeft"><?php echo "ABF (เด็ก)";?></td>
    <td class="pRight"><?php $AbfC = $AbfCRow['more_information_value']; echo $AbfC;?></td>
    <td class="pRight"><?php echo bath_format($AbfCRow['more_information_price']);?></td>
    <td class="pRight"><?php $AbfCTotal = $AbfC*$AbfCRow['more_information_price']; echo bath_format($AbfCTotal);?></td>
  </tr>
<?php } ?>
<?php 
//กรณีหากมีข้อมูล พักเพิ่ม (ผู้ใหญ่)
$AddAQuery = mysql_query("SELECT more_information_value,more_information_price FROM reservation_information WHERE reservation_detail_id = '$id' AND more_information_id = '6'");
$AddARow = mysql_fetch_array($AddAQuery);
if($AddARow){
	$NumAddA = 1;
?>
  <tr>
    <td class="center"><?php echo $num+$NumExtraAbf+$NumtABF+$NumExtra+$NumAbfA+$NumAbfC+$NumAddA;?></td>
    <td class="pLeft"><?php echo "พักเพิ่ม (ผู้ใหญ่)";?></td>
    <td class="pRight"><?php $AddA = $AddARow['more_information_value']; echo $AddA;?></td>
    <td class="pRight"><?php echo bath_format($AddARow['more_information_price']);?></td>
    <td class="pRight"><?php $AddATotal = $AddA*$AddARow['more_information_price']; echo bath_format($AddATotal);?></td>
  </tr>
<?php } ?>
<?php 
//กรณีหากมีข้อมูล พักเพิ่ม (เด็ก)
$AddCQuery = mysql_query("SELECT more_information_value,more_information_price FROM reservation_information WHERE reservation_detail_id = '$id' AND more_information_id = '7'");
$AddCRow = mysql_fetch_array($AddCQuery);
if($AddCRow){
	$NumAddC = 1;
?>
  <tr>
    <td class="center"><?php echo $num+$NumExtraAbf+$NumtABF+$NumExtra+$NumAbfA+$NumAbfC+$NumAddC+$NumAddA;?></td>
    <td class="pLeft"><?php echo "พักเพิ่ม (เด็ก)";?></td>
    <td class="pRight"><?php $AddC = $AddCRow['more_information_value']; echo $AddC;?></td>
    <td class="pRight"><?php echo bath_format($AddCRow['more_information_price']);?></td>
    <td class="pRight"><?php $AddCTotal = $AddC*$AddCRow['more_information_price']; echo bath_format($AddCTotal);?></td>
  </tr>
<?php } ?>
  <tr>
	<td colspan="4" class="right">รวมเงิน</td>
	<td class="pRight"><?php $strtalAll = $strtal+$tABFTotal+$ExtraAbfTotal+$ExtraTotal+$AbfATotal+$AbfCTotal+$AddATotal+$AddCTotal; echo bath_format($strtalAll);?></td>
  </tr>
<?php 
//แสดงเงินรวมหลังลดแล้วกรณี W/N
if($swRow['reservation_detail_payment_information']=='walkin'){
	$DisQuery = mysql_query("SELECT discount,discount2 FROM reservation_detail WHERE reservation_detail_id = '$id'");
	$DisRow = mysql_fetch_array($DisQuery);
	$strtalAll = $strtalAll - $DisRow['discount2'];
?>
<tr>
    <td colspan="4" class="right">ส่วนลด % : <?php echo $DisRow['discount'];?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ส่วนลดเป็นเงิน <?php echo bath_format($DisRow['discount2']);?></td>
    <td class="pRight"><?php echo bath_format($strtalAll);?></td>
</tr>
<?php } ?>
  <tr>
      <td colspan="4" class="right"><font style="border-bottom: 1px solid #000000;"><b>ค่าเงินมัดจำ</b></font></td>
    <td class="pRight"><font style="border-bottom: 1px solid #000000;"><b>- <?php echo bath_format($totalA); ?> </b></font></td>
</tr>
    <tr>
        <td colspan="4" class="right"><font style="border-bottom: 3px double #000000;"><b>เหลือเงินค้างจ่าย</b></font></td>
        <td class="pRight"><font style="border-bottom: 3px double #000000;"><b><?php $Atotal = $strtalAll-$totalA; echo bath_format($Atotal); ?></b></font></td>
    </tr>
</table>
<?php } ?>
</div>
<?php
if($tbl == 'deposit_cash' && $zRow['deposit_cash_percent'] != 0){
?>
    หมายเหตุ : จ่ายค่ามัดจำ เป็นเงินสดจำนวน <?php echo $zRow['deposit_cash_percent']; ?>%
<?php
} else if($tbl == 'deposit_transfer'){
    $BankQuery = mysql_query("SELECT bank_name FROM bank WHERE bank_id = '".$zRow['bank_id']."'");
    $BankRow = mysql_fetch_array($BankQuery);
?>
   หมายเหตุ : โอนเงินเข้า <?php echo $BankRow['bank_name']; ?>
 <?php
} else if($tbl == 'deposit_check'){
    
?>
   หมายเหตุ : จ่ายเช็คเลขที่ <?php echo $zRow['deposit_check_number']; ?> &nbsp;&nbsp;ธนาคาร <?php echo $zRow['deposit_check_bank']; ?>
 <?php
}
?>
</div>
<div style="padding-top: 20px;width: 100%; padding-bottom:20px; text-align:center;">
<input type="button" name="endsale" id="endsale" value="พิมพ์ใบจอง" onclick="javascript:window.print();Print();" >
<input type="button" name="endsale" disabled="disabled" id="endsale2" value="กลับหน้าหลัก" onclick="javascript:window.location='deposit.php';" >
</div>
</div>
</body>
</html>